package com.xuecheng.checkcode.service.impl;

import com.xuecheng.checkcode.model.CheckCodeParamsDto;
import com.xuecheng.checkcode.model.CheckCodeResultDto;
import com.xuecheng.checkcode.service.CheckCodeService;
import com.xuecheng.checkcode.service.SendCodeByPhoneService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shiminghui
 * @date 2024/9/9 20:23
 * @description: TODO
 */
@Service
@RequiredArgsConstructor
public class SendCodeByPhoneServiceImpl implements SendCodeByPhoneService {

    @Resource(name = "NumberCheckCodeService")
    private CheckCodeService checkCodeService;


    @Override
    public CheckCodeResultDto sendCodeByPhone(CheckCodeParamsDto checkCodeParamsDto) {
        String param1 = checkCodeParamsDto.getParam1();

        String regex1 = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[1589]))\\d{8}$";
        String regex2 = "^(([^<>()[\\]\\\\.,;:\\s@\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";

        if (param1.matches(regex1)) {//手机号
            System.out.println("向手机发送验证码");
        } else if (param1.matches(regex2)) {//邮箱
            System.out.println("向邮箱发送验证码");
        } else {
            throw new RuntimeException("手机号或邮箱不符合格式");
        }
        CheckCodeResultDto resultDto = checkCodeService.generate(checkCodeParamsDto);

        return resultDto;
    }
}
